﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using TecBaseObjects.Interfaces;
using TecBaseObjects.Models.BusinessCases;
using TecData.Manager;
using TpOffice.Forms.BusinessCase;
using TpOffice.Interfaces;
using TecExtensions;
using TecBaseObjects.Objects;

namespace TpOffice.Controller {

    public class BusinessCasesShortListController : IController {

        #region Members

        BusinessCasesShortListForm myForm;

        #endregion

        #region Constructor

        public BusinessCasesShortListController(BusinessCasesShortListForm form) {
            this.myForm = form;
        }

        #endregion

        #region Methods

        private void ChangedSelectProperty(object sender, PropertyChangedEventArgs e) {
            if (e.PropertyName == "Intern"
                || e.PropertyName == "CreatedFrom"
                || e.PropertyName == "CreatedTo"
                || e.PropertyName == "CustomerGroup"
                || e.PropertyName == "CustomerSource"
                || e.PropertyName == "FileUserID"
                || e.PropertyName == "ProjectAddress"
                || e.PropertyName == "ProjectEPLZ"
                || e.PropertyName == "ProjectStatus"
                || e.PropertyName == "Purchaser"
                ) {
                ReloadMyModel();
                this.myForm.SetFormDataSources();
            }
            
        }

        public void CreateNewBusinessCase(object sender, EventArgs e) {
            BusinessCaseMainForm bcm = new BusinessCaseMainForm(0);
            bcm.ShowDialog();
            this.ReloadMyModel();
        }

        public void Dispose() {
        }

        public void LoadModels() {
            BusinessCasesSelectModel selectModel = new BusinessCasesSelectModel();
            selectModel.PropertyChanged += new PropertyChangedEventHandler(ChangedSelectProperty);
            this.myForm.MySelectModel = selectModel;
            this.myForm.MyBusinessCaseStatusModel = BusinessCaseDataManager.LoadBusinessCaseStatusList(true);
            this.myForm.MyCustomerSourceModel = BusinessCaseDataManager.LoadCustomerSourceList(true);
            this.myForm.MyPurchaserModel = CommonDataManager.LoadOrganisationsShortList();
            this.myForm.MyPlzOrtModel = CommonDataManager.LoadPlzOrtList(true);
            this.myForm.MyFileUserModel = TecUserDataManager.LoadTecUser(true);
            this.myForm.MyCustomerGroupModel = CommonDataManager.LoadCustomerGroupList();

            ReloadMyModel();
        }

        public void OpenBusinessCase(object sender, DataGridViewCellMouseEventArgs e) {
            int rowIndex = e.RowIndex;
            BusinessCaseMainForm frm = new BusinessCaseMainForm(this.myForm.MyModel[rowIndex].ProjectID);
            frm.ShowDialog();

            if (!frm.MyModel.IsDirty) {
                this.myForm.MyModel[rowIndex].CostSummary = frm.MyModel.CostSummary;
                this.myForm.MyModel[rowIndex].CreatedBy = frm.MyModel.CreatedBy;
                this.myForm.MyModel[rowIndex].CreatedOn = frm.MyModel.CreatedOn;
                this.myForm.MyModel[rowIndex].CustomerGroup = frm.MyModel.CustomerGroup;
                this.myForm.MyModel[rowIndex].CustomerSource = BusinessCaseDataManager.LoadCustomerSource(frm.MyModel.CustomerSource).CustomerSourceShort;
                this.myForm.MyModel[rowIndex].ErloesSumNetto = frm.MyModel.ErloesSumNetto;
                this.myForm.MyModel[rowIndex].FileUser = TecUserDataManager.LoadTecUser(frm.MyModel.FileUserID).LoginName;
                this.myForm.MyModel[rowIndex].FinishDate = frm.MyModel.FinishDate;
                this.myForm.MyModel[rowIndex].FirstContactDate = frm.MyModel.FirstContactDate;
                this.myForm.MyModel[rowIndex].ImplementationDate = frm.MyModel.ImplementationDate;
                this.myForm.MyModel[rowIndex].Intern = frm.MyModel.Intern;
                Organisation org = OrganisationDataManager.LoadOrganisation(frm.MyModel.InvoiceRecipient);
                this.myForm.MyModel[rowIndex].InvoiceRecipient = org.Prefix.AddSpace() + org.OrgName;
                this.myForm.MyModel[rowIndex].LastChangedBy = frm.MyModel.LastChangedBy;
                this.myForm.MyModel[rowIndex].LastChangedOn = frm.MyModel.LastChangedOn;
                this.myForm.MyModel[rowIndex].OrderDate = frm.MyModel.OrderDate;
                this.myForm.MyModel[rowIndex].ProjectAddress = frm.MyModel.ProjectAddress;
                this.myForm.MyModel[rowIndex].ProjectStatus = frm.MyModel.ProjectStatus;
                this.myForm.MyModel[rowIndex].ProjectStatusText = BusinessCaseDataManager.LoadBusinessCaseStatus((byte)frm.MyModel.ProjectStatus).StateText;
                Organisation pur = OrganisationDataManager.LoadOrganisation(frm.MyModel.Purchaser);
                this.myForm.MyModel[rowIndex].Purchaser = pur.Prefix.AddSpace() + pur.OrgName;
                this.myForm.MyModel[rowIndex].ResultDB = frm.MyModel.ResultDB;
                this.myForm.MyModel[rowIndex].ShortName = frm.MyModel.ShortName;
                this.myForm.MyModel[rowIndex].TimeSummary = frm.MyModel.TimeSummary;
            }
        }

        public void ReloadMyModel() {
            this.myForm.MyModel = BusinessCaseDataManager.LoadBusinessCasesShortList(this.myForm.MySelectModel);
            this.myForm.SetFormDataSources();
        }

        public void Save(IBaseObject myObject) {
            throw new NotImplementedException("BusinessCasesShortListController.Save");
        }

        #endregion

    }
}
